#include "stm32f10x.h" //STM32头文件
#include "sys.h"
#include "menu.h"
#include <stdio.h>
#include "delay.h"
#include "oled.h"
//#include "bmp.h"
#include "buzzer.h"
#include "ds18b20.h"
#include "TM1638.h"
#include "l298n.h"
#include "usart.h"
#include "tim.h"
#include "pid.h"
u8 speed=100,speed2=100;
float A=OFF,num;
short  temperature;
unsigned int TEMP;						//储存到小数一位的实际实时温度
unsigned int SETTEMP;					//储存到小数一位的实际设置温度
unsigned int PWM=2500;
u8 flag100ms=0;
extern u8 airc;
extern u8 fanc;
extern u8 mode;
extern u8 flag1min;
extern unsigned int  set_temp;
unsigned char flag500ms=0;
int main (void){//主程序
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	BUZZER_Init();
	delay_init();
	TM1638_Init(); //TM1638初始化函数
	DS18B20_Init();
	OLED_Init();	
	Motor_12_init();
//	USART_Init_2(9600);
	USART_Init_2(115200);
	TIM3_PWM_Init(10000-1,72-1);
	TIM4_Int_Init(100-1,720-1);//每毫秒产生中断
//	TIM3_PWM_Init(999,0);
	USART2_IRQHandler();
	TIM4_IRQHandler();   //TIM4中断服务函数
	PID_Init();

	while(1){//主循环

		loop();
		TEMP=DS18B20_Get_Temp();
		fan_pid();
	}
}


void air_pid()//空调pid
{
	  
	  if(mode==ON){cold();PID_Init2();}
		else if(mode==OFF){hot();PID_Init3();	}

	  SETTEMP=set_temp*10;
    TIM_SetCompare3(TIM3,PWM);
		 if(flag100ms)										//每100ms更新一次实时温度
		{
			flag100ms=0;
			flag500ms++;
			if(flag500ms==5)                //500ms测量一次pid温度
			{
				flag500ms=0;
				if(mode==ON){PWM=PID_realize2();}//制冷
		    else if(mode==OFF){PWM=PID_realize();}//制热
				
				TIM_SetCompare3(TIM3,PWM);
			}
		}
		TIM_SetCompare3(TIM3,PWM);
}

void fan_pid()//风扇pwm调速
{
   unsigned int PWM2;
	 fan();
   switch(fanc)
				{
					case 0:PWM2=0;
							break;
					case 1:PWM2=2500;
							break;
					case 2:PWM2=5000;
							break;
					case 3:PWM2=7500;
							break;
					case 4:PWM2=9999;
              break;
					default:break;
				}
   TIM_SetCompare4(TIM3,PWM2);
}
//**************************************************************************
/*
【变量定义】
u32     a; //定义32位无符号变量a
u16     a; //定义16位无符号变量a
u8     a; //定义8位无符号变量a
vu32     a; //定义易变的32位无符号变量a
vu16     a; //定义易变的 16位无符号变量a
vu8     a; //定义易变的 8位无符号变量a
uc32     a; //定义只读的32位无符号变量a
uc16     a; //定义只读 的16位无符号变量a
uc8     a; //定义只读 的8位无符号变量a

#define ONE  1   //宏定义

delay_us(1); //延时1微秒
delay_ms(1); //延时1毫秒
delay_s(1); //延时1秒

*/
